package com.yanghx.product.controller;

import com.yanghx.product.service.ProductService;
import com.yanghx.product.common.DecreaseStockInput;
import com.yanghx.product.common.ProductInfoOutput;
import com.yanghx.product.entity.ProductCategory;
import com.yanghx.product.entity.ProductInfo;
import com.yanghx.product.service.CategoryService;
import com.yanghx.product.vo.ProductInfoVO;
import com.yanghx.product.vo.ProductVO;
import com.yanghx.product.vo.ResultVOUtil;
import com.yanghx.product.vo.ResultVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yangHX
 * createTime  2019/9/11 10:11
 */
@Api(value = "商品信息")
@RestController
@RequestMapping(value = "/product")
public class ProductController {

    @Resource
    private ProductService productService;
    @Resource
    private CategoryService categoryService;

    @PostMapping(value = "/test")
    public String test() {
        return "then is product . for test feign ";
    }


    @GetMapping("/list")
    public ResultVo<List<ProductVO>> list() {
        //1. 查询所有在架的商品
        List<ProductInfo> productInfoList = productService.findUpAll();
        //2. 获取类目type列表
        List<Integer> categoryTypeList = productInfoList.stream().map(ProductInfo::getCategoryType).collect(Collectors.toList());
        //3. 从数据库查询类目
        List<ProductCategory> categoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        //4. 构造数据
        List<ProductVO> productVOList = categoryList.stream().map(productCategory -> {
            ProductVO productVO = ProductVO.builder().categoryName(productCategory.getCategoryName()).categoryType(productCategory.getCategoryType()).build();
            List<ProductInfoVO> productInfoVOList = productInfoList.stream()
                    .filter(productInfo -> productInfo.getCategoryType().equals(productCategory.getCategoryType()))
                    .map(item -> {
                        ProductInfoVO productInfoVO = new ProductInfoVO();
                        BeanUtils.copyProperties(item, productInfoVO);
                        return productInfoVO;
                    }).collect(Collectors.toList());
            productVO.setProductInfoVOList(productInfoVOList);
            return productVO;
        }).collect(Collectors.toList());
        return ResultVOUtil.success(productVOList);
    }


    /**
     * 获取商品列表（给订单服务用）
     *
     * @param productIdList 商品idList
     * @return List
     */
    @PostMapping("/listForOrder")
    public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList) {
        return productService.findList(productIdList);
    }

    /**
     * 扣库存
     *
     * @param decreaseStockInputList 需要被库存的数据
     */
    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList) {
        productService.decreaseStock(decreaseStockInputList);
    }

}
